﻿<!DOCTYPE html>

<html>
<head>
    <title>Order Page</title>
    <style>
        .thumbnail 
        {
            width: 50px;
            height: 50px;
        }
    </style>
    <script type="text/javascript">
        function expand(photo_url) {
            imgWindow = window.open("", "Full Size");
            imgWindow.document.write(<div><img id="Image" src=photo_url alt="Full Image" /></div>);
        }

        function calcTotal(max){
            var sum=0;
            for (var i=0; i<max; i++){
                var prodObj=document.getElementById("input" + i);
                var amt=prodObj.value;
                if (isInteger(amt) && amt>0){
                    //Not sure how to access the PHP vars.
                    //Set quantity to amt.
                    //sum+=amt * price;
                }
            }
        }
    </script>
</head>
<body>
    <div> Initial setup (activated when first enetering the page):
    SQL Script:
    $query=$this->db->query("
        select *
        from product
        ")

        PHP:
    $itemArray=arr();
    for ($i=0; $i<$query->num_rows(); $i++){
        $tuple=$query->row_array($i);
        $item=new Item($tuple[0], $tuple[1], $tuple[2], $tuple[3], $tuple[4]);
        $itemArray[]=$item;
    }

    Object:
    class Item{
        public function Item($pID, $name, $desc, $photo, $price){
            $this->productID=$pID;
            $this->name = $name;
            $this->desc = $desc;
            $this->photo = $photo;
            $this->price = $price;
            $this->quantity=0;
        }
    }
    </div>

    <div> Actual page:
    echo "<table>";
    echo "<tr> <th>Product</th> <th>Description</th> <th>Image</th> <th>Price</th> <th>Quantity</th> </tr>";
    for ($i=0; $i<$query->num_rows(); $i++){
        $product=$itemArray[$i];
        $idName="input"+$i;
        echo "<tr> <td>$product->name</td> <td>$product->desc</td> <td><img class="thumbnail" src=$product->photo onclick="expand($product->photo)"/></td> <td>$product->price</td> <td><input id=$idName type="text" name="amount" value=0/></td></tr>";
    }
    echo "</table>"
    echo "<input type="button" value="Checkout" onclick="calcTotal($query->num_rows())"/>";
    </div>

    <div> Checkout part:
    The sum is calculated by a script (loop through every text field and .get its value).
    Also, add a quantity field to product, and update it through the script if the quantity in the text field is not 0.
    Generate an error message for invalid input.
    Generate a new order id.

    for ($i=0; $i<count($itemArray); $i++){
        if ($itemArray[$i]->quantity > 0){
            $data=array(
            "id" => $order_item_id++,
            "order_id" => $order_id++,
            "product_id" => $itemArrray[$i]->productID,
            "quantity" => $itemArray[$i]->quantity,
            );
            $this->db->insert("order_item", $data);
        }
    }

    Then, insert the order into the database.
    Prompt for a valid credit card.
    Show the receipt.
    Use the current time and date, and get the total from the script.
    </div>
</body>
</html>
